package com.whl.org.service.impl;

import com.whl.basic.service.impl.BaseServiceImpl;
import com.whl.basic.util.MD5Utils;
import com.whl.basic.util.StrUtils;
import com.whl.org.domain.Employee;
import com.whl.org.mapper.EmployeeMapper;
import com.whl.org.service.IEmployeeService;
import com.whl.user.domain.Logininfo;
import com.whl.user.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.List;

/**
 * 员工业务实现类
 */
@Service
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements IEmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private LogininfoMapper logininfoMapper;

    /**
     * 添加重构
     * @param employee
     */
    @Override
    public void add(Employee employee) {
        // 1.处理盐值
        String salt = StrUtils.getComplexRandomString(32);
        // 2.调用md5工具类，给密码加密加盐
        String md5Password = MD5Utils.encrypByMd5(employee.getPassword() + salt);
        employee.setSalt(salt);
        employee.setPassword(md5Password);
        Logininfo logininfo = employee2Logininfo(employee);
        logininfoMapper.save(logininfo);
        employee.setLogininfo_id(logininfo.getId());
        super.add(employee);
        // 选了角色
        if(employee.getRole_id() != null){
            employeeMapper.addEmployeeRole(employee.getId(),employee.getRole_id());
        }
    }

    private Logininfo employee2Logininfo(Employee employee) {
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(employee,logininfo);
        // 当前对象是管理员
        logininfo.setType(0);
        return logininfo;
    }

    /**
     * 修改重构
     * @param employee
     */
    @Override
    public void update(Employee employee) {
        // 1.处理盐值
        String salt = StrUtils.getComplexRandomString(32);
        // 2.调用md5工具类，给密码加密加盐
        String md5Password = MD5Utils.encrypByMd5(employee.getPassword() + salt);
        employee.setSalt(salt);
        employee.setPassword(md5Password);
        Logininfo logininfo = employee2Logininfo(employee);
        // 3.获取logininfo_id
        Long logininfo_id = employeeMapper.findOne(employee.getId()).getLogininfo_id();
        logininfo.setId(logininfo_id);
        logininfoMapper.update(logininfo);
        // 登录信息id和店铺信息id不能修改
        super.update(employee);

        // 先删除角色
        employeeMapper.deleteEmployeeRole(employee.getId());
        // 再添加角色
        if(employee.getRole_id() != null){
            employeeMapper.addEmployeeRole(employee.getId(),employee.getRole_id());
        }
    }

    /**
     * 删除重构
     * @param id
     */
    @Override
    public void delete(Long id) {
        // 根据id查询到employee
        Employee employee = employeeMapper.findOne(id);
        // 删除登录信息表数据
        logininfoMapper.delete(employee.getLogininfo_id());
        // 删除角色
        employeeMapper.deleteEmployeeRole(id);

        // 删除employee表数据
        super.delete(id);
    }

    @Override
    public List<Employee> list(Long shopId) {
        return employeeMapper.list(shopId);
    }
}
